package PaChong;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 14:02:00 2018/10/31
 * @version:2.0
 */
public class ProductImg {
    public String imgName;
    public String imgUrlOrigin;
    public String imgUrlIndex;
    public String imgUrlBig;
    public String updateDate;

    public static List getAllProductImg(Product product, Category category) {
        List<ProductImg> productImgs = new ArrayList();
        String html = "";
        try {
            html = Html.getHtml(product.productUrl);
        } catch (IOException e) {
            System.out.println(product.productName + ":" + product.productUrl);
            return null;
        }
        Document doc = Jsoup.parse(html);
        Elements beizhus = doc.select("div.showalbumheader__gallerysubtitle");//"备注
        Iterator<Element> beizhusIt = beizhus.iterator();
        while (beizhusIt.hasNext()) {
            Element beizhu = beizhusIt.next();
            product.beizhu = beizhu.html();
        }
        Elements doms = doc.select("div.showalbum__children");//"信息区域
        Iterator<Element> domsIt = doms.iterator();
        while (domsIt.hasNext()) {
            Element dom = domsIt.next();
            ProductImg productImg = new ProductImg();

            Elements pictures = dom.select("img[data-origin-src]");//"图片
            Iterator<Element> picturesIt = pictures.iterator();
            while (picturesIt.hasNext()) {
                Element picture = picturesIt.next();
                productImg.imgUrlOrigin = picture.attr("data-origin-src");
                productImg.imgUrlBig = picture.attr("data-src");
                productImg.imgUrlIndex = productImg.imgUrlBig.replaceAll("big", "small").replaceAll("medium", "small");
            }

            Elements title = dom.select("h3");//名字
            Iterator<Element> titleIt = title.iterator();
            while (titleIt.hasNext()) {
                Element titleInfo = titleIt.next();
                productImg.imgName = titleInfo.text();
            }

            Elements time = dom.select("time");//更新时间
            Iterator<Element> timeIt = time.iterator();
            while (timeIt.hasNext()) {
                Element timeInfo = timeIt.next();
                productImg.updateDate = timeInfo.text();
            }
            productImgs.add(productImg);
        }
        if (productImgs.size() == 0) {
            category.isSuccess = true;
        }
        product.isSuccess = true;
        category.isSuccess = true;
        product.productImgs = productImgs;
        return productImgs;
    }


}
